package com.xukang.aes.utils;

import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.net.URLEncoder;

public class AESUtil2 {

    private static final String KEY = "gdwpcryptelcihev";
    private static final String IV = "shuygagcgshuisiv";
    private static final String KEY_ALGORITHM = "AES";
    private static final String DEFAULT_CIPHER_ALGORITHM = "AES/CBC/PKCS5Padding";//默认的加密算法

    public static String Encrypt(String sSrc) throws Exception {
        byte[] raw = KEY.getBytes("utf-8");
        SecretKeySpec skeySpec = new SecretKeySpec(raw, KEY_ALGORITHM);
        Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);//"算法/模式/补码方式"
        IvParameterSpec iv = new IvParameterSpec(IV.getBytes());//使用CBC模式，需要一个向量iv，可增加加密算法的强度
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
        byte[] encrypted = cipher.doFinal(sSrc.getBytes());
        return Base64.encodeBase64String(encrypted);//此处使用BASE64做转码功能，同时能起到2次加密的作用。
    }

    public static String Decrypt(String sSrc) throws Exception {
        try {
            byte[] raw = KEY.getBytes("utf-8");
            SecretKeySpec skeySpec = new SecretKeySpec(raw, KEY_ALGORITHM);
            Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
            IvParameterSpec iv = new IvParameterSpec(IV.getBytes());
            cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
            byte[] encrypted1 = new Base64().decode(sSrc);//先用base64解密
            try {
                byte[] original = cipher.doFinal(encrypted1);
                String originalString = new String(original, "UTF-8");
                return originalString;
            } catch (Exception e) {
                System.out.println(e.toString());
                return null;
            }
        } catch (Exception ex) {
            System.out.println(ex.toString());
            return null;
        }
    }

    public static void main(String[] args) throws Exception {
        String data = "C206A00123194EDE6376955B044CA1B6D5D8E2A4A65B7F3BA78E50DB61B9379C65A20F3C00307BB72B4106A922465856EA3A4CDEB4E4D8B0D38B888F230D9C533B4495CF29AAAAFCB6FD470F319401BE22890B2BA1046136FE8416177FDE32430BF436180D2FA80126F8B661AF1479A15EC6B98373D22469C2EF1D0BE756FF2F911888B2AD08242AE3287C2048BE7134190278AD905A27021EDE01E54F2B90F366AB457D945BA3107EDDEAF2B683D0C8FB88DC73A63A1F92FEF0E77936AF93206DAD0B6E0E73B7C061CE13974A4BE25D50CAEC87A2E9A88884E5724C9D39324786C8B55534E520347079685FC4F758D1A7E99C2939B1EE2B3A778BA2B359F99A09C1ABC8C4009B518F9E27CE18944888A559A767ED630FF98C82AE894F43458AFA1A35A9EE81E1B227B7C7C2B0329478182EE19A09B33FD483D4070F4F40502123D91CF0A7D42849A10FD168BCF89FE4103F776F4692048454F74D5E00CEE34433FC0F37B29EC579A3B7CF8A8B3F97CDACA5D2DD63DDF126214D4516E50F023B52A4E7F281CA46E9EFEC3089458DFFAA84684845BCEF457151D8CDDBE27B685C96572D332722B133C738C0F605B2CD73F4E5DB1DF7A25B1218A127D3B9F37E29C02B0A3CCE820B2382516472745996EA36934F08191EF9555A8C4D754B4279EBE77951DD7651D6870D83C7723853B6A901B1FDBA9ED413680FBC29BC2250E1C00CB2514FDC1412B52F35537AAA7252CFAB6A5DCAA18A17CE4A1168885C78ADBA0EBDDBC880C9B91CBE046A00817EACD20ED64CF508FD6C9102E9FA1084521AFA4A6F367A2A07A2FF";
        String enData = AESUtil2.Encrypt(data);
        System.out.println("加密后的数据为：" + enData);
        String ec = URLEncoder.encode(enData, "UTF-8");
        System.out.println("加密后的数据为：" + ec);
        String deData = AESUtil2.Decrypt(enData);
        System.out.println("解密后的数据为：" + deData);
    }
}
